﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;

using FinalProject.BL;
using FinalProject.DAL.Model;

namespace FinalProject.UI.Controls
{
    public class OrderDocumentGrid : DataGrid
    {
        public DataGridTextColumn colID, colDocNumber, colCreateDate, colCustomerCode, colTotalAmount, colCurrency, colDescription;

        public OrderDocumentGrid()
            : base()
        {
            this.InitializeColums();
            this.IsReadOnly = true;
            this.CanSelectMultipleItems = false;
            this.AutoGenerateColumns = false;
            this.CanUserAddRows = false;
            this.CanUserDeleteRows = false;
            this.SelectionMode = DataGridSelectionMode.Single;
            this.SelectionUnit = DataGridSelectionUnit.FullRow;
        }

        private void InitializeColums()
        {
            this.colID = new DataGridTextColumn()
            {
                Header = "Id",
                Visibility = System.Windows.Visibility.Hidden
            };
            this.Columns.Add(this.colID);

            this.colDocNumber = new DataGridTextColumn()
            {
                Header = "Number",
                Binding = new Binding("DocNumber"),
                Width = 100
            };
            this.Columns.Add(this.colDocNumber);

            this.colCreateDate = new DataGridTextColumn()
            {
                Header = "Date",
                Binding = new Binding("DocDate"),
                Width = 150
            };
            this.Columns.Add(this.colCreateDate);

            this.colCustomerCode = new DataGridTextColumn()
            {
                Header = "Customer",
                Binding = new Binding("Customer.Code"),
                Width = 100
            };
            this.Columns.Add(this.colCustomerCode);

            this.colTotalAmount = new DataGridTextColumn()
            {
                Header = "Total amount",
                Binding = new Binding("TotalAmount"),
                Width = 80
            };
            this.Columns.Add(this.colTotalAmount);

            this.colDescription = new DataGridTextColumn()
            {
                Header = "Description",
                Binding = new Binding("Description"),
                Width = 250
            };
            this.Columns.Add(this.colDescription);
        }

        public Guid GetSelectedId()
        {
            OrderDocument selectedItem = this.SelectedValue as OrderDocument;
            if (selectedItem == null) return Guid.Empty;
            return selectedItem.Id;
        }

        public void RefreshData()
        {
            this.ItemsSource = OrderManagementBL.GetAll();
        }
    }
}
